﻿@using Ext.Net
@{
    ViewBag.Title = "Permission List";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section script
{
    <script type="text/javascript">
        function handlerCommand(command, record) {
            if (command == 'Edit') {
                window.App.winEdit.loader.url = '@Url.Action("Edit",
                                                             new
                                                                 {
                                                                         id = "ID"
                                                                 })'.replace('ID', record.data.Id);
                window.App.winEdit.show();
                window.App.winEdit.load();
            } else if (command == 'Delete')
                window.Ext.Msg.show({
                    title: "Confirm",
                    buttons: window.Ext.Msg.YESNO,
                    fn: function(btn) {
                        if (btn == 'yes')
                            request(
                                '@Url.Action("Delete")',
                                { id: record.data.Id },
                                function(result) {
                                    showNotification(window, result, function() {
                                        window.App.gvData.getStore().reload();
                                    });
                                },
                                function() {
                                    window.Ext.Msg.show(
                                        {
                                            title: "Confirm",
                                            buttons: window.Ext.Msg.YES,
                                            icon: window.Ext.Msg.ERROR,
                                            msg: "There some errors occour when update. Please try again later."
                                        });
                                });
                    },
                    icon: window.Ext.Msg.QUESTION,
                    msg: "Do you really want to delete this record?"
                });
        }

        function add() {
            window.App.winAdd.show();
            window.App.winAdd.load();
        }
    </script>
}
@(Html.X()
      .Viewport()
      .Layout(LayoutType.Border)
      .Items(view =>
                 {
                     view.Add(Html.X()
                                  .Toolbar()
                                  .Region(Region.North)
                                  .Items(c =>
                                             {
                                                 c.Add(Html.X()
                                                           .ToolbarFill());
                                                 c.Add(Html.X()
                                                           .Button()
                                                           .Text("Add Permission")
                                                           .Icon(Icon.Add)
                                                           .OnClientClick("add"));
                                             }));
                     view.Add(Html.X()
                                  .GridPanel()
                                  .View(Html.X().GridView().EnableTextSelection(true))
                                  .ID("gvData")
                                  .Region(Region.Center)
                                  .ForceFit(true)
                                  .ColumnModel(grid =>
                                                   {
                                                       grid.Add(Html.X()
                                                                    .Column()
                                                                    .Text("Group Name")
                                                                    .Hidden(true)
                                                                    .DataIndex("GroupName"));
                                                       grid.Add(Html.X()
                                                                    .Column()
                                                                    .Text("Name")
                                                                    .DataIndex("Name"));
                                                       grid.Add(Html.X()
                                                                    .Column()
                                                                    .Text("KeyName")
                                                                    .DataIndex("KeyName"));
                                                       grid.Add(Html.X()
                                                                    .Column()
                                                                    .Text("Description")
                                                                    .DataIndex("Description"));
                                                       grid.Add(Html.X()
                                                                    .Column()
                                                                    .Width(20)
                                                                    .PaddingSpec("0 10")
                                                                    .Align(Alignment.Center)
                                                                    .Commands(c =>
                                                                                  {
                                                                                      c.Add(new ImageCommand
                                                                                                {
                                                                                                        Icon = Icon.TableEdit,
                                                                                                        CommandName = "Edit",
                                                                                                });
                                                                                      c.Add(new ImageCommand
                                                                                                {
                                                                                                        Icon = Icon.Delete,
                                                                                                        CommandName = "Delete",
                                                                                                });
                                                                                  })
                                                                    .Listeners(c =>
                                                                                   {
                                                                                       c.Command.Handler = "handlerCommand(command, record);";
                                                                                   }));
                                                   })
                                  .Store(Html.X()
                                             .Store()
                                             .RemoteSort(true)
                                             .RemotePaging(true)
                                             .RemoteGroup(true)
                                             .PageSize(50)
                                             .AutoLoad(true)
                                             .AutoSync(false)
                                             .GroupField("GroupName")
                                             .Model(c => c.Add(Html.X()
                                                                   .Model()
                                                                   .Fields("Id",
                                                                           "Name",
                                                                           "Description",
                                                                           "KeyName",
                                                                           "GroupName")
                                                                   .IDProperty("Id")))
                                             .Proxy(c => c.Add(Html.X()
                                                                   .AjaxProxy()
                                                                   .Json(true)
                                                                   .Url(Url.Action("GetList"))
                                                                   .ActionMethods(v => v.Read = HttpMethod.POST)
                                                                   .Reader(Html.X()
                                                                               .JsonReader()
                                                                               .Root("data")
                                                                               .TotalProperty("total"))))
                                             .AutoLoadParams(c =>
                                                                 {
                                                                     c.Add(new Parameter("start",
                                                                                         "0",
                                                                                         ParameterMode.Raw));
                                                                     c.Add(new Parameter("limit",
                                                                                         "50",
                                                                                         ParameterMode.Raw));
                                                                 }))
                                  .Features(v => v.Add(Html.X()
                                                           .Grouping()
                                                           .GroupHeaderTplString("{name} ({[values.rows.length]} {[values.rows.length > 1 ? \"items\" : \"item\"]})")))
                                  .BottomBar(c => c.Add(new PagingToolbar())));
                 }))
@(Html.X()
      .Window()
      .ID("winAdd")
      .Resizable(false)
      .Modal(true)
      .Header(true)
      .Width(450)
      .Height(280)
      .Hidden(true)
      .Loader(Html.X()
                  .ComponentLoader()
                  .Url(Url.Action("Add"))
                  .Mode(LoadMode.Frame)
                  .AutoLoad(false)))
@(Html.X()
      .Window()
      .ID("winEdit")
      .Resizable(false)
      .Modal(true)
      .Header(true)
      .Width(450)
      .Height(280)
      .Hidden(true)
      .Loader(Html.X()
                  .ComponentLoader()
                  .Url(Url.Action("Edit"))
                  .Mode(LoadMode.Frame)
                  .AutoLoad(false)))